﻿using UnityEngine;
using System.Collections;



public class ViewBall : View
{

    public override void Update()
    {
        if (model.IsInValid())
        {
            this.LazyDispose();
            return;
        }
        this._obj_pos.position = new Vector3(model.x, model.y, this._obj_pos.position.z);
        if (model.alive)
        {
            this._obj_ball.localScale = new Vector3(model.scale, model.scale, 1.0f);
        }

        if (model.alive != _cache_alive)
        {// lazy set active
            _cache_alive = model.alive;
            this._obj_pos.gameObject.SetActive(model.alive);
            _cache_alive = model.alive;
        }



        float x = model.x_delta * 1.7f;
        float y = model.y_delta * 1.7f;

        this._obj_dir.localPosition = new Vector3(x, y, this._obj_dir.position.z);
        this._obj_dir.localRotation = Quaternion.Euler(new Vector3(0.0f, 0.0f, model.dir - 270.0f));

    }

    public override bool Init()
    {
        base.Init();
        this._root = PrefabsMgr.Load(DATA.PREFABS_FILE_BALL);
        this._obj_pos = _root.transform;

        this._obj_ball = this._obj_pos.FindChild("Ball").transform;
        this._obj_name = this._obj_pos.FindChild("name").transform;
        this._obj_dir = this._obj_pos.FindChild("dir").transform;

        this.model = this._model as Ball;
        _root.name = model.no.ToString();
        //  _root.tag = model.no.ToString();

        this._obj_ball.GetComponent<SpriteRenderer>().color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));

        this._obj_name.GetComponent<TextMesh>().text = model.name;   //"玩家:" + model.no;

        this._obj_dir = this._obj_pos.FindChild("dir");
        return true;
    }

    public override void OnEvent(string type, object userData)
    {

    }

    public override void OnExit()
    {
        ViewMgr.ins.Remove(this);
        PrefabsMgr.Destroy(this._root);
      //  Debug.Log("Ball View exit");
        base.OnExit();
    }

    private Transform _obj_pos;
    private Transform _obj_name;
    private Transform _obj_ball;
    private Transform _obj_dir;
    private new  Ball model;

    private bool _cache_alive = true;

}



